package com.zking.web.admin.tea_admin;

import com.zking.entity.PageManager;
import com.zking.entity.Teacher;
import com.zking.service.TeacherService;
import com.zking.utils.NumberUtils;
import com.zking.web.BaseServlet;
import sun.misc.Request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/teacher")
public class TeacherServlet extends BaseServlet {

    private TeacherService teaService = new TeacherService();

    protected void  service(HttpServletRequest request,HttpServletResponse response)  throws ServletException, IOException {
        String m = getM(request);
        //根据传来的m跳转界面
        switch (m){
            case "index" : index(request,response);break;
            case "add" : add(request,response);break;
            case "del" : del(request,response);break;
            case "query" : query(request,response);break;
            case "update" : update(request,response);break;

        }
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String TEACHER_NAME = request.getParameter("TEACHER_NAME");
        String NICK_NAME = request.getParameter("NICK_NAME");
        String ID_CARD_NO = request.getParameter("ID_CARD_NO");
        String MOBILE_PHONE = request.getParameter("MOBILE_PHONE");
        Double MONTHLY_SALARY = NumberUtils.parseDouble(request.getParameter("MONTHLY_SALARY"));
        Double HEIGHT = NumberUtils.parseDouble(request.getParameter("HEIGHT"));
        Double WEIGHT = NumberUtils.parseDouble(request.getParameter("WEIGHT"));
        String NATION = request.getParameter("NATION");
        String DOMICILE_PLACE_PROVINCE = request.getParameter("DOMICILE_PLACE_PROVINCE");
        String DOMICILE_PLACE_CITY = request.getParameter("DOMICILE_PLACE_CITY");
        String DOMICILE_PLACE_ADDRESS = request.getParameter("DOMICILE_PLACE_ADDRESS");
        String HOBBY = request.getParameter("HOBBY");
        String PRESENT_ADDRESS = request.getParameter("PRESENT_ADDRESS");
        String EMAIL = request.getParameter("EMAIL");
        String GENDER = request.getParameter("GENDER");
        String BIRTH = request.getParameter("BIRTH");
        String GRADUATE_INSTITUTION = request.getParameter("GRADUATE_INSTITUTION");
        Integer PRACTICE_YEARS = NumberUtils.parseInt(request.getParameter("PRACTICE_YEARS"));
        String POLITICAL = request.getParameter("POLITICAL");
        String MARITAL = request.getParameter("MARITAL");
        String AVATAR = request.getParameter("AVATAR");
        String INTRO = request.getParameter("INTRO");
        Integer TEACHER_ID = NumberUtils.parseInt(request.getParameter("TEACHER_ID"));
        Teacher teacher = new Teacher(TEACHER_ID,TEACHER_NAME,NICK_NAME,ID_CARD_NO,MOBILE_PHONE,MONTHLY_SALARY,HEIGHT,WEIGHT,
                NATION,DOMICILE_PLACE_PROVINCE,DOMICILE_PLACE_CITY,DOMICILE_PLACE_ADDRESS,HOBBY,PRESENT_ADDRESS,EMAIL,GENDER,BIRTH,GRADUATE_INSTITUTION,PRACTICE_YEARS,POLITICAL,MARITAL,AVATAR,INTRO);
        boolean update = teaService.updateTea(teacher);
        HttpSession session = request.getSession();
        session.setAttribute("result",update);
        session.setAttribute("msg",update? "修改成功" : "修改失败");
        PageManager pm = (PageManager) session.getAttribute("pm");
        //回到首页
        response.sendRedirect("tea?index&pn="+pm.getPn());
    }

    private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Integer TEACHER_ID = NumberUtils.parseInt(request.getParameter("TEACHER_ID"));
        Teacher teacher = teaService.queryTea(TEACHER_ID);
        HttpSession session = request.getSession();
        request.setAttribute("teacher" , teacher);
        request.getSession().setAttribute("secondName","修改老师信息");
        //进入修改页面
        dispatcher("updateTea",request,response);
    }

    private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Integer TEACHER_ID = NumberUtils.parseInt(request.getParameter("TEACHER_ID"));
        boolean delTea = teaService.delTea(TEACHER_ID);
        HttpSession session = request.getSession();
        session.setAttribute("result", delTea);
        session.setAttribute("msg",delTea ? "删除成功" : "删除失败");
        //获取pm，重新计算
        PageManager pm = (PageManager) session.getAttribute("pm");
        pm.setTotalCount(pm.getTotalCount()-1);
        //重新计算页数
        int newPages = pm.calPages(pm.getTotalCount(),pm.getRn());
        pm.setPages(newPages);
        pm.setPn(pm.getPn() > pm.getPages()? pm.getPages() : pm.getPn());
        //覆盖域中的pm
        session.setAttribute("pm",pm);
        //跳转到首页
        response.sendRedirect("tea?m=index&pn="+pm.getPn());
    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String TEACHER_NAME = request.getParameter("TEACHER_NAME");
        String NICK_NAME = request.getParameter("NICK_NAME");
        String ID_CARD_NO =request.getParameter("ID_CARD_NO");
        String MOBILE_PHONE = request.getParameter("MOBILE_PHONE");
        String EMAIL = request.getParameter("EMAIL");
        //将这些数据封装到实体类
        Teacher teacher = new Teacher(TEACHER_NAME,NICK_NAME,ID_CARD_NO,MOBILE_PHONE,EMAIL);
        //将数据传递到Service中
        boolean addResult = teaService.addTea(teacher);
        //将查询结果设置到域中
        HttpSession session = request.getSession();
        session.setAttribute("result",addResult);
        session.setAttribute("msg",addResult ? "增加成功" : "增加失败");
        //获取当前的pm,重写计算
        PageManager pm = (PageManager) session.getAttribute("pm");
        pm.setTotalCount(pm.getTotalCount()+1);
        //重新计算页数
        int newPages = pm.calPages(pm.getRn(),pm.getPages());
        pm.setPages(newPages);
        session.setAttribute("pm",pm);
        //回到首页
        response.sendRedirect("tea?m=index&pn="+pm.getPages());
    }

    /**
     * 访问首页
     */
    private void index(HttpServletRequest request,HttpServletResponse response)  throws ServletException, IOException{
        //创建分页管理器对象
        int pn = NumberUtils.parseInt(request.getParameter("pn"));
        int rn = NumberUtils.parseInt(request.getParameter("rn"));
        //获取总数据数
        int totalCount = teaService.getTeacherTotal();
        //获取分页数据
        PageManager pm = new PageManager(pn,rn,totalCount);
        //查询对应的数据
        List<Teacher> teachers = teaService.queryTeachers(pm);
        //将数据存储到request域中
        //分页数据存储session域中，四哥参数全部在pm对象中
        request.getSession().setAttribute("pm",pm);
        //存储老师数据
        request.setAttribute("teacher",teachers);
        //分页需要访问的路径
        request.setAttribute("url","admin/teacher?m=index");
        //转发
        dispatcher("teacher",request,response);
    }

}
